//
//  HNSH_FeedbackHeaderView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/7.
//

#import "HNSH_FeedbackHeaderView.h"

//V
#import "UITextView+Placeholder.h"

@interface HNSH_FeedbackHeaderView ()<UITextViewDelegate>
@property (nonatomic, strong)UIView * typeView;
@property (nonatomic, strong)UILabel * typeLabel;
@property (nonatomic, strong)UIImageView * typeImgView;
@property (nonatomic, strong)UIView * phoneView;
@property (nonatomic, strong)UITextField * phoneTextField;
@property (nonatomic, strong)UIView * detailView;
@property (nonatomic, strong)UITextView * detailTextView;
@property (nonatomic, strong)UILabel * countLabel;
@property (nonatomic, strong)UILabel * titleLabel;
@end

@implementation HNSH_FeedbackHeaderView

//MARK: Cycle Methods
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

- (void)addAllLayout{
    [self.typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.mas_equalTo(self.typeView);
        make.left.mas_equalTo(16);
    }];
    [self.typeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.typeView);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(5);
        make.right.mas_equalTo(-16);
    }];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.typeView.mas_bottom).offset(1);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.mas_equalTo(self.phoneView);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
    }];
    [self.detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneView.mas_bottom).offset(1);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(160);
    }];
    [self.detailTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(14);
        make.left.mas_equalTo(15);
        make.bottom.mas_equalTo(-10);
        make.right.mas_equalTo(-15);
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-10);
        make.height.mas_equalTo(20);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.detailView.mas_bottom).offset(14);
        make.left.mas_equalTo(16);
        make.height.mas_equalTo(22);
    }];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self addSubview:self.typeView];
    [self.typeView addSubview:self.typeLabel];
    [self.typeView addSubview:self.typeImgView];
    [self addSubview:self.phoneView];
    [self.phoneView addSubview:self.phoneTextField];
    [self addSubview:self.detailView];
    [self.detailView addSubview:self.detailTextView];
    [self.detailView addSubview:self.countLabel];
    [self addSubview:self.titleLabel];
}

- (void)gesAction{
    if (self.chooseTypeBlock) {
        self.chooseTypeBlock();
    }
}

- (NSString *)phone{
    return self.phoneTextField.text;
}

- (NSString *)detail{
    return self.detailTextView.text;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (textView == self.detailView && text.length && (textView.text.length + text.length) > 200) {
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView{
    self.countLabel.text = [NSString stringWithFormat:@"%ld/200", textView.text.length];
}

//MARK: Setter Methods
- (void)setType:(NSString *)type{
    _type = type;
    
    if ([type isNotBlank]) {
        self.typeLabel.text = type;
    } else {
        self.typeLabel.text = @"请选择反馈类型";
    }
}

//MARK: Getter Methods
- (UIView *)typeView{
    if (!_typeView) {
        _typeView = [UIView new];
        _typeView.backgroundColor = [UIColor whiteColor];
        
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction)];
        [_typeView addGestureRecognizer:ges];
    }
    return _typeView;
}

- (UILabel *)typeLabel{
    if (!_typeLabel) {
        _typeLabel = [UILabel new];
        _typeLabel.font = [UIFont systemFontOfSize:16];
        _typeLabel.textColor = [UIColor appBlack333LabelColor];
        _typeLabel.text = @"请选择反馈类型";
    }
    return _typeLabel;
}

- (UIImageView *)typeImgView{
    if (!_typeImgView) {
        _typeImgView = [UIImageView new];
        _typeImgView.image = [UIImage imageNamed:@"mine_bill_down_icon"];
    }
    return _typeImgView;
}

- (UIView *)phoneView{
    if (!_phoneView) {
        _phoneView = [UIView new];
        _phoneView.backgroundColor = [UIColor whiteColor];
    }
    return _phoneView;
}

- (UITextField *)phoneTextField{
    if (!_phoneTextField) {
        _phoneTextField = [UITextField new];
        _phoneTextField.font = [UIFont systemFontOfSize:16];
        _phoneTextField.textAlignment = NSTextAlignmentLeft;
        _phoneTextField.placeholder = @"请填写手机号，方便我们联系您";
        _phoneTextField.textColor = [UIColor appBlackLabelColor];
        NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:_phoneTextField.placeholder];
        [placeholderString addAttribute:NSForegroundColorAttributeName
                                value:[UIColor colorWithHexString:@"#8E8E93"]
                                range:NSMakeRange(0, _phoneTextField.placeholder.length)];
        [placeholderString addAttribute:NSFontAttributeName
                                value:[UIFont systemFontOfSize:14]
                                range:NSMakeRange(0, _phoneTextField.placeholder.length)];
        _phoneTextField.attributedPlaceholder = placeholderString;
    }
    return _phoneTextField;
}

- (UIView *)detailView{
    if (!_detailView) {
        _detailView = [UIView new];
        _detailView.backgroundColor = [UIColor whiteColor];
    }
    return _detailView;
}

- (UITextView *)detailTextView{
    if (!_detailTextView) {
        _detailTextView = [UITextView new];
        _detailTextView.font = [UIFont systemFontOfSize:16];
        _detailTextView.textAlignment = NSTextAlignmentLeft;
        _detailTextView.delegate = self;
        _detailTextView.textColor = [UIColor appBlackLabelColor];
        _detailTextView.placeholder = @"您在使用APP过程中遇到了什么问题，或者对平台 有什么建议，都可以写在这里哦";
        _detailTextView.placeholderColor = [UIColor colorWithHexString:@"#8E8E93"];
        _detailTextView.backgroundColor = [UIColor appWhiteLabelColor];
    }
    return _detailTextView;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [UILabel new];
        _countLabel.font = [UIFont systemFontOfSize:16];
        _countLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _countLabel.textAlignment = NSTextAlignmentRight;
        _countLabel.text = @"0/200";
    }
    return _countLabel;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor appBlack333LabelColor];
        _titleLabel.text = @"上传图片（最多6张）";
    }
    return _titleLabel;
}

@end
